package com.liyunc.socket.webclient;


import com.liyunc.socket.webclient.util.Request;
import com.liyunc.socket.webclient.util.Response;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class HttpThread extends Thread {

    //html和其他文件存放的目录
    public static final String WEB_ROOT =
        System.getProperty("user.dir") + File.separator + "webroot";

    private final Socket socket;

    public HttpThread(Socket socket) {
        this.socket = socket;
    }

    public void await() throws IOException {
        InputStream input = socket.getInputStream();
        OutputStream output = socket.getOutputStream();

        // 创建Request对象并解析
        Request request = new Request(input);
        String xxx = request.getParameter("xxx");
        String yyy = request.getParameter("yyy");

        // 创建 Response 对象
        Response response = new Response(output);
        if ("aaa".equals(xxx) && "bbb".equals(yyy)) {
            response.send(200, "success!!!");
        } else {
            response.send(404, "no found");
        }

        // 关闭 socket 对象
        socket.close();
    }

    @Override
    public void run() {
        try {
            await();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
